home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-09-09 | 8.4 KB | 269 lines | [TEXT/CWIE] |
- // Simple framework for Macintosh sample code
- //
- // David Hayward and Nick Thompson
- // Developer Technical Support
- // AppleLink: DEVSUPPORT
- //
- // Copyrite 1994, Apple Computer,Inc
- //
- // Application menu handling code such as dimming menu items and
- // dispatching items. The dispatcher first calls the the MenuProcPtr
- // of the frontmost window to see if it could handle the event. Any
- // items not handled by the front windo are handled here.
- //
- // 9/13/94 nick first cut
- // 9/13/94 nick implement commands
- // 12/13/94 david several modifications
- // 8/23/95 david added generic prining to the framework
-
-
- #include <Types.h>
- #include <StandardFile.h>
- #include <Devices.h>
-
- #include "appGlobals.h"
- #include "appMain.h"
- #include "appMenus.h"
- #include "appErrors.h"
- #include "appPrint.h"
- #include "appAEvts.h"
-
- #include "win.h"
- #include "winTables.h"
-
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE FUNCTION PROTOTYPES
- |**| ==============================================================================
- \**/
- void DoAppNewCommand ( void ) ;
- void DoAppOpenCommand ( void ) ;
- void DoAppCloseCommand ( winHandle win ) ;
- void DoAppQuitCommand ( void ) ;
-
-
- /**\
- |**| ==============================================================================
- |**| PUBLIC FUNCTIONS
- |**| ==============================================================================
- \**/
-
-
- /*------------------------------------------------------------------------------*\
- dimAllMenus
- *------------------------------------------------------------------------------*
- This routine dims all menus and menu items in the menu bar
- It is called by:
- InitMenuBar() which is called when the app is intited,
- DoActivateEvent() which is called whenever a window is brought to front,
- AdjustMenusForPrinting() which is called before GX print dialogs, and
- CloseProcPtrs which are called whenever windows are closed
- \*------------------------------------------------------------------------------*/
- void dimAllMenus ( void )
- {
- short menu, item, totalItems ;
- MenuHandle mhdl ;
-
- for ( menu=mFirstMenu; menu <= mLastMenu; menu++ )
- {
- mhdl = GetMenuHandle( menu ) ;
- DisableItem( mhdl, kWholeMenu ) ;
-
- if (menu==mApple)
- totalItems = 1 ;
- else
- totalItems = CountMItems(mhdl) ;
-
- for ( item=1; item<=totalItems; item++ )
- DisableItem( mhdl, item ) ;
- }
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoAppAdjustMenus
- *------------------------------------------------------------------------------*
- This routine enables any menus and menu items in the menu bar
- which are able to be handled.
- It is called by:
- InitMenuBar() which is called when the app is intited,
- DoActivateEvent() which is called whenever a window is brought to front, and
- CloseProcPtrs which are called whenever windows are closed
- \*------------------------------------------------------------------------------*/
- void DoAppAdjustMenus ( void )
- {
- MenuHandle theMenu ;
- short i, openableTypes ;
-
- dimAllMenus() ;
-
- // let the windows enable the items they can handle
- (void) CallAllWinUpdateMenusProcs() ;
-
- // Call default UpdateMenusProc
- gDefaulltUpdateMenusProc( nil ) ;
-
- // let the famework enable the items it can handle
- for ( i=openableTypes=0; i<gAllocProcMapCount; i++ )
- if (gAllocProcMap[i].filetype != nil )
- openableTypes++;
-
- theMenu = GetMenuHandle ( mFile ) ;
- EnableItem ( theMenu, kWholeMenu ) ;
- if (openableTypes) EnableItem ( theMenu, iOpen ) ;
- EnableItem ( theMenu, iQuit ) ;
-
- DrawMenuBar() ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- HandleMenuCommand
- *------------------------------------------------------------------------------*
- This routine handles all commands generated by mouse event or
- by command key equivalent. This routine first calls the the MenuProcPtr
- of the frontmost window to see if it could handle the event. After that
- it hadles any menu items which the application is responsible for.
- It is called by:
- DoMouseDownEvent() which is called in response to mouse events, and
- DoKeyDownEvent() which is called in response to keyboard events
- \*------------------------------------------------------------------------------*/
- void HandleMenuCommand ( long menuResult )
- {
- short menuID ;
- short menuItem ;
- Str255 daName ;
- winHandle win ;
- Boolean didit = false ;
-
- // see if any of the windows can handle the event
- (void) CallAllWinMenuProcs( menuResult, &didit) ;
- if (didit) return ; // check to see if a window handled it
-
- // Call default MenuProc
- gDefaulltMenuProc(nil, menuResult, &didit) ;
- if (didit) return ; // check to see if the default handled it
-
- // if that fails, let the famework do what it can
- win = GetWindowWinHandle( FrontWindow() ) ;
- menuID = HiWrd(menuResult) ;
- menuItem = LoWrd(menuResult) ;
- switch ( menuID )
- {
- case mApple:
- GetMenuItemText(GetMenuHandle(mApple), menuItem, daName) ;
- OpenDeskAcc(daName) ;
- break;
-
- case mFile:
- switch ( menuItem )
- {
- case iOpen: // open an existing file
- DoAppOpenCommand() ;
- break ;
-
- case iClose: // close an open window/document
- DoAppCloseCommand( win ) ;
- break ;
-
- #if PIGS_SHELL_PRINT
- case iPageSetup: // std page setup dialog
- DoAppPageSetupCommand( win ) ;
- break ;
-
- case iPrint: // std print job dialog
- DoAppPrintCommand( win ) ;
- break ;
-
- case iPrintOne: // std print job dialog
- DoAppPrintOneCommand( win ) ;
- break ;
- #endif
- case iQuit:
- DoAppQuitCommand() ;// send ourselves a quit event
- break ;
- }
- break ;
- }
- HiliteMenu(0) ; // Unhighlight whatever MenuSelect or MenuKey hilited
- }
-
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE FUNCTIONS
- |**| ==============================================================================
- \**/
-
-
- /*------------------------------------------------------------------------------*\
- DoAppOpenCommand
- *------------------------------------------------------------------------------*
- This routine handles the File:Open command for the application.
- It calls StandardGetFile() and with the resulting FSSpec, calls
- SendODOC() to force the app to send an ODOC event to itself.
- It is called by:
- HandleMenuCommand() which handles all menu events.
- \*------------------------------------------------------------------------------*/
- static void DoAppOpenCommand ( void )
- {
- SFTypeList *myTypesPtr ;
- StandardFileReply theSFReply ; // Get the file name to open
- short i, count ;
- OSErr err ;
-
- myTypesPtr = (SFTypeList*) NewPtr( (Size)( sizeof(long) * gAllocProcMapCount) ) ;
- err = MemError();
- WarnIfErr( err ) ;
- if (err) return ;
-
- for ( i=count=0; i<gAllocProcMapCount; i++ )
- if (gAllocProcMap[i].filetype != nil )
- (*myTypesPtr)[count++] = gAllocProcMap[i].filetype ;
-
- StandardGetFile( nil, count, *myTypesPtr, &theSFReply ) ;
-
- DisposePtr( (Ptr)myTypesPtr ) ;
-
- if(theSFReply.sfGood) // if the user did not cancel
- SendODOC( &theSFReply.sfFile ) ;// send ODOC event to ourselves:
-
- return ;
- }
-
-
-
- /*------------------------------------------------------------------------------*\
- DoAppCloseCommand
- *------------------------------------------------------------------------------*
- This routine handles the File:Close command for the application.
- It calls DisposeWinHandle() and also returns all the menus
- to the default state by calling DoAppAdjustMenus().
- This is needed because, if the window being closed is the only window,
- then there is not another window to trigger an activate event.
- It is called by:
- HandleMenuCommand() which handles all menu events.
- \*------------------------------------------------------------------------------*/
- static void DoAppCloseCommand ( winHandle win )
- {
- CallWinCloseProc( win ) ;
- DoAppAdjustMenus() ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoAppQuitCommand
- *------------------------------------------------------------------------------*
- This routine handles the File:Quit command for the application.
- It calls StandardGetFile() and with the resulting FSSpec, calls
- It calls SendQUIT() to force the app to send an QUIT event to itself.
- It is called by:
- HandleMenuCommand() which handles all menu events.
- \*------------------------------------------------------------------------------*/
- static void DoAppQuitCommand ( void )
- {
- SendQUIT() ; // send ourselves a quit event
- return ;
- }
-